//
//  HomeViewTableViewCell.swift
//  WeiBo
//
//  Created by wukeng on 16/9/1.
//  Copyright © 2016年 wukeng. All rights reserved.
//

import UIKit
import SDWebImage

private let edgeMargin : CGFloat = 15
private let itemMargin : CGFloat = 10

class HomeViewTableViewCell: UITableViewCell {
    //MARK:- 控件属性
    @IBOutlet weak var iconImage: UIImageView!
    @IBOutlet weak var verifiedImage: UIImageView!
    @IBOutlet weak var screenName: UILabel!
    @IBOutlet weak var vipImage: UIImageView!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var sourceLabel: UILabel!
    @IBOutlet weak var contentLabel: UILabel!
    @IBOutlet weak var collectView: PicCollectionView!
    //MARK:- 约束属性
    @IBOutlet weak var contentLabelCons: NSLayoutConstraint!
    @IBOutlet weak var collectViewHeightCons: NSLayoutConstraint!
    @IBOutlet weak var collectViewWidthCons: NSLayoutConstraint!
    var viewModel : StatusViewModel?{
        didSet{
            //1.空值校验
            guard let viewModel = viewModel else{
                return
            }
            //2.设置头像
            iconImage.sd_setImageWithURL(viewModel.profileURL, placeholderImage: UIImage(named: "avatar_default_small"))
            //3.设置认证的图标
            verifiedImage.image = viewModel.verifiedImage
            //4.设置昵称
            screenName.text = viewModel.status?.user?.screen_name
            //5.会员图标
            vipImage.image = viewModel.vipImage
            //6.设置时间的Label
            timeLabel.text = viewModel.createdAtText
            //7.设置来源
            contentLabel.text = viewModel.status?.text
            //8.设置来源label
            sourceLabel.text = viewModel.sourceText
            //9.设置昵称颜色
            screenName.textColor = ((viewModel.status?.user?.mbrank > 0) ? UIColor.orangeColor() : UIColor.blackColor())
            //10.计算collectView的宽度高度的约束
            let collectSize = calculateCollectViewSize(viewModel.picURLs.count)
            collectViewWidthCons.constant = collectSize.width
            collectViewHeightCons.constant = collectSize.height
            //11.将collectView的数据传递给PicCollectView
            collectView.picURLs = viewModel.picURLs
        
        }
    }
    
    //MARK:- 系统回调函数
    override func awakeFromNib() {
        super.awakeFromNib()
        //设置微博正文的宽度约束
        contentLabelCons.constant = UIScreen.mainScreen().bounds.height - 2 * edgeMargin
        //取出collectView对应的layout
        let layout = collectView.collectionViewLayout as! UICollectionViewFlowLayout
        let itemH = (UIWidth - 2 * edgeMargin - 2 * itemMargin) / 3
        layout.itemSize = CGSize(width: itemH - 1, height: itemH - 1)
    }

}

// MARK: - 计算collectView的宽高
extension HomeViewTableViewCell{
    private func calculateCollectViewSize(count : Int) -> CGSize{
        //1.没有配图
        if count == 0 {
            return CGSizeZero
        }
        //1.1单张配图
        if count == 1{
            //取出图片
            let urlString = viewModel?.picURLs.last?.absoluteString
            let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(urlString)
            return image.size
        }
        //2.4张配图
        let itemH = (UIWidth - 2 * edgeMargin - 2 * itemMargin) / 3
        if count == 4 {
            let collectViewH = itemH * 2 + itemMargin
            return CGSize(width: collectViewH, height: collectViewH)
        }
        //3.其他张配图
        let rows = (CGFloat)((count - 1) / 3 + 1)
        let collectViewH = itemH * rows + (rows - 1) * itemMargin
        let collectViewW = UIWidth - 2 * edgeMargin
        return CGSize(width: collectViewW, height: collectViewH)
    }
}












